<?php
/**
 * Clase Base FN para BaseImArticuloFN
 *
 * @package    
 * @subpackage BaseImArticuloFN
 * @author     hans
 * @version    SVN:: BaseImArticuloFN.class.php,v 1.1 2012-03-16 23:50:58 Hans.parra Exp $
 */

require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/im/articulos/BN/ImArticuloBN.class.php');

class BaseImArticuloFN extends ImArticuloBN
{
  private static $fieldNames = array (
    'ArticuloId'=>'itm_articuloId', 'Tipo'=>'itm_tipo', 'Descripcion'=>'itm_descripcion', 'Memo'=>'itm_memo', 'Alias'=>'itm_alias', 'CodFabricante'=>'itm_codFabricante', 'ClasificId'=>'itm_clasificId', 'SubClasificId'=>'itm_subClasificId', 'MarcaId'=>'itm_marcaId', 'Proveed1'=>'itm_proveed1', 'Proveed2'=>'itm_proveed2', 'UndMedida'=>'itm_UoM', 'UndMedidaCompra'=>'itm_UoMCompra', 'UoMFactor'=>'itm_UoMCompraFacto', 'UoMVenta'=>'itm_UoMVenta', 'UoMVentaFactor'=>'itm_UoMVentaFacto', 'Garantia'=>'itm_garantia', 'Inventariable'=>'itm_inventariable', 'Serializable'=>'itm_serializable', 'ParaCompra'=>'itm_paraCompra', 'ParaVenta'=>'itm_paraVenta', 'Impuesto1Sw'=>'itm_impuesto1_sw', 'Impuesto2Sw'=>'itm_impuesto2_sw', 'Impuesto3Sw'=>'itm_impuesto3_sw', 'Advalorem'=>'itm_advalorem', 'Peso'=>'itm_peso', 'Volumen'=>'itm_volumen', 'PrecioDefault'=>'itm_precioDefault', 'CostoProm'=>'itm_costoProm', 'CostoUlt'=>'itm_costoUlt', 'StockMin'=>'itm_stockMin', 'StockMax'=>'itm_stockMax', 'TiempoReposic'=>'itm_tiempoReposic', 'Activo'=>'itm_activo', 'CreaUsr'=>'itm_crea_usr', 'CreaFecha'=>'itm_crea_fecha', 'ModifUsr'=>'itm_modif_usr', 'ModifFecha'=>'itm_modif_fecha'  
  );

  public function select()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con=$o_conexion->conectar();
    	
      $tamPag = !property_exists($this, 'tamPag')?9999:$this->tamPag;
      $pagina = !property_exists($this, 'page')||$this->page==""?1:$this->page;
      $this->numeroRegistros = $this->selectNumRows();
      $limitInf = ($pagina-1)*$tamPag;
    	
      $where = $this->buildConsult();
      //$orden = $this->buildOrderByField();
      $orden="";
      $sql = "SELECT itm_articuloId, itm_tipo, itm_descripcion, itm_memo, itm_alias, itm_codFabricante, itm_clasificId, itm_subClasificId, itm_marcaId, itm_proveed1, itm_proveed2, itm_UoM, itm_UoMCompra, itm_UoMCompraFacto, itm_UoMVenta, itm_UoMVentaFacto, itm_garantia, itm_inventariable, itm_serializable, itm_paraCompra, itm_paraVenta, itm_impuesto1_sw, itm_impuesto2_sw, itm_impuesto3_sw, itm_advalorem, itm_peso, itm_volumen, itm_precioDefault, itm_costoProm, itm_costoUlt, itm_stockMin, itm_stockMax, itm_tiempoReposic, itm_activo, itm_crea_usr, itm_crea_fecha, itm_modif_usr, itm_modif_fecha FROM im_articulo WHERE 1 ".$where." ".$orden."  LIMIT ".$limitInf.",".$tamPag;
      //$sql = "SELECT itm_articuloId, itm_tipo, itm_descripcion, itm_memo, itm_alias, itm_codFabricante, itm_clasificId, itm_subClasificId, itm_marcaId, itm_proveed1, itm_proveed2, itm_UoM, itm_UoMCompra, itm_UoMCompraFacto, itm_UoMVenta, itm_UoMVentaFacto, itm_garantia, itm_inventariable, itm_serializable, itm_paraCompra, itm_paraVenta, itm_impuesto1_sw, itm_impuesto2_sw, itm_impuesto3_sw, itm_advalorem, itm_peso, itm_volumen, itm_precioDefault, itm_costoProm, itm_costoUlt, itm_stockMin, itm_stockMax, itm_tiempoReposic, itm_activo, itm_crea_usr, itm_crea_fecha, itm_modif_usr, itm_modif_fecha FROM im_articulo WHERE 1 ".$where;
      $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      $a_tmp = array();
      if($num_rows>0){
        while ($o_valor = mysql_fetch_object($result)) {
          $a_tmp[] = $this->insertObjectBN($o_valor);
        }
      }
      
      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function selectNumRows()
  {
    try
    {
      $where = $this->buildConsult();

      $o_conexion = new BaseDatos();
      $con=$o_conexion->conectar();
      $sql = "SELECT itm_articuloId, itm_tipo, itm_descripcion, itm_memo, itm_alias, itm_codFabricante, itm_clasificId, itm_subClasificId, itm_marcaId, itm_proveed1, itm_proveed2, itm_UoM, itm_UoMCompra, itm_UoMCompraFacto, itm_UoMVenta, itm_UoMVentaFacto, itm_garantia, itm_inventariable, itm_serializable, itm_paraCompra, itm_paraVenta, itm_impuesto1_sw, itm_impuesto2_sw, itm_impuesto3_sw, itm_advalorem, itm_peso, itm_volumen, itm_precioDefault, itm_costoProm, itm_costoUlt, itm_stockMin, itm_stockMax, itm_tiempoReposic, itm_activo, itm_crea_usr, itm_crea_fecha, itm_modif_usr, itm_modif_fecha FROM im_articulo WHERE 1 ".$where;
        $oLink = method_exists($con, 'getResource')?$con->getResource():$con;
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      
      return $num_rows;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildConsult()
  {
    try
    {
      $where = "";

      if($this->getArticuloId()){ $where .= " AND itm_articuloid = '".$this->getArticuloId()."'"; }
      if($this->getTipo()){ $where .= " AND itm_tipo = '".$this->getTipo()."'"; }
      if($this->getDescripcion()){ $where .= " AND itm_descripcion LIKE '".$this->getDescripcion()."%'"; }
      if($this->getMemo()){ $where .= " AND itm_memo = '".$this->getMemo()."'"; }
      if($this->getAlias()){ $where .= " AND itm_alias = '".$this->getAlias()."'"; }
      if($this->getCodFabricante()){ $where .= " AND itm_codfabricante = '".$this->getCodFabricante()."'"; }
      if($this->getClasificId()){ $where .= " AND itm_clasificid = '".$this->getClasificId()."'"; }
      if($this->getSubClasificId()){ $where .= " AND itm_subclasificid = '".$this->getSubClasificId()."'"; }
      if($this->getMarcaId()){ $where .= " AND itm_marcaid = '".$this->getMarcaId()."'"; }
      if($this->getProveed1()){ $where .= " AND itm_proveed1 = '".$this->getProveed1()."'"; }
      if($this->getProveed2()){ $where .= " AND itm_proveed2 = '".$this->getProveed2()."'"; }
      if($this->getUndMedida()){ $where .= " AND itm_uom = '".$this->getUndMedida()."'"; }
      if($this->getUndMedidaCompra()){ $where .= " AND itm_uomcompra = '".$this->getUndMedidaCompra()."'"; }
      if($this->getUoMFactor()){ $where .= " AND itm_uomcomprafacto = '".$this->getUoMFactor()."'"; }
      if($this->getUoMVenta()){ $where .= " AND itm_uomventa = '".$this->getUoMVenta()."'"; }
      if($this->getUoMVentaFactor()){ $where .= " AND itm_uomventafacto = '".$this->getUoMVentaFactor()."'"; }
      if($this->getGarantia()){ $where .= " AND itm_garantia = '".$this->getGarantia()."'"; }
      if($this->getInventariable()){ $where .= " AND itm_inventariable = '".$this->getInventariable()."'"; }
      if($this->getSerializable()){ $where .= " AND itm_serializable = '".$this->getSerializable()."'"; }
      if($this->getParaCompra()){ $where .= " AND itm_paracompra = '".$this->getParaCompra()."'"; }
      if($this->getParaVenta()){ $where .= " AND itm_paraventa = '".$this->getParaVenta()."'"; }
      if($this->getImpuesto1Sw()){ $where .= " AND itm_impuesto1_sw = '".$this->getImpuesto1Sw()."'"; }
      if($this->getImpuesto2Sw()){ $where .= " AND itm_impuesto2_sw = '".$this->getImpuesto2Sw()."'"; }
      if($this->getImpuesto3Sw()){ $where .= " AND itm_impuesto3_sw = '".$this->getImpuesto3Sw()."'"; }
      if($this->getAdvalorem()){ $where .= " AND itm_advalorem = '".$this->getAdvalorem()."'"; }
      if($this->getPeso()){ $where .= " AND itm_peso = '".$this->getPeso()."'"; }
      if($this->getVolumen()){ $where .= " AND itm_volumen = '".$this->getVolumen()."'"; }
      if($this->getPrecioDefault()){ $where .= " AND itm_preciodefault = '".$this->getPrecioDefault()."'"; }
      if($this->getCostoProm()){ $where .= " AND itm_costoprom = '".$this->getCostoProm()."'"; }
      if($this->getCostoUlt()){ $where .= " AND itm_costoult = '".$this->getCostoUlt()."'"; }
      if($this->getStockMin()){ $where .= " AND itm_stockmin = '".$this->getStockMin()."'"; }
      if($this->getStockMax()){ $where .= " AND itm_stockmax = '".$this->getStockMax()."'"; }
      if($this->getTiempoReposic()){ $where .= " AND itm_tiemporeposic = '".$this->getTiempoReposic()."'"; }
      if($this->getActivo()){ $where .= " AND itm_activo = '".$this->getActivo()."'"; }
      if($this->getCreaUsr()){ $where .= " AND itm_crea_usr = '".$this->getCreaUsr()."'"; }
      if($this->getCreaFecha()){ $where .= " AND itm_crea_fecha = '".$this->getCreaFecha()."'"; }
      if($this->getModifUsr()){ $where .= " AND itm_modif_usr = '".$this->getModifUsr()."'"; }
      if($this->getModifFecha()){ $where .= " AND itm_modif_fecha = '".$this->getModifFecha()."'"; }
      return $where;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildOrderByField()
  {
    try
    {
      $order_by = "";

      if(property_exists($this,'sortCampos')){
      if($this->sortCampos != ""){
        $a_valCampos = explode(";", $this->sortCampos);
        $a_valtype   = explode(";", $this->sortType);
        $order_by = " ORDER BY ";
        $a_order = array();
        foreach ($a_valCampos as $key => $val){
          $sTmpOrder = "";
          if(array_key_exists($val, self::$fieldNames)){
            $sTmpOrder .= self::$fieldNames[$val];
            switch ($a_valtype[$key]){
              case 1: $sTmpOrder .= " ASC"; break;
              case 2: $sTmpOrder .= " DESC"; break;
            }
            $a_order[] = $sTmpOrder;
          }
        }
        $order_by .= implode(", ", $a_order);
      }
      }
      return $order_by;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* B�squeda del registro por la llave primaria */
  public function selectByPK()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con=$o_conexion->conectar();
      $sql = "SELECT itm_articuloId, itm_tipo, itm_descripcion, itm_memo, itm_alias, itm_codFabricante, itm_clasificId, itm_subClasificId, itm_marcaId, itm_proveed1, itm_proveed2, itm_UoM, itm_UoMCompra, itm_UoMCompraFacto, itm_UoMVenta, itm_UoMVentaFacto, itm_garantia, itm_inventariable, itm_serializable, itm_paraCompra, itm_paraVenta, itm_impuesto1_sw, itm_impuesto2_sw, itm_impuesto3_sw, itm_advalorem, itm_peso, itm_volumen, itm_precioDefault, itm_costoProm, itm_costoUlt, itm_stockMin, itm_stockMax, itm_tiempoReposic, itm_activo, itm_crea_usr, itm_crea_fecha, itm_modif_usr, itm_modif_fecha FROM im_articulo WHERE 1 AND itm_articuloId='".$this->getArticuloId()."'; ";
        $oLink = method_exists($con, 'getResource')?$con->getResource():$con;
        $result = mysql_query($sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      

      return $o_Propel?$this->insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_BaseImArticuloFN = new ImArticuloBN();
    $o_BaseImArticuloFN->setArticuloId($o_propel->itm_articuloId);
    $o_BaseImArticuloFN->setTipo($o_propel->itm_tipo);
    $o_BaseImArticuloFN->setDescripcion($o_propel->itm_descripcion);
    $o_BaseImArticuloFN->setMemo($o_propel->itm_memo);
    $o_BaseImArticuloFN->setAlias($o_propel->itm_alias);
    $o_BaseImArticuloFN->setCodFabricante($o_propel->itm_codFabricante);
    $o_BaseImArticuloFN->setClasificId($o_propel->itm_clasificId);
    $o_BaseImArticuloFN->setSubClasificId($o_propel->itm_subClasificId);
    $o_BaseImArticuloFN->setMarcaId($o_propel->itm_marcaId);
    $o_BaseImArticuloFN->setProveed1($o_propel->itm_proveed1);
    $o_BaseImArticuloFN->setProveed2($o_propel->itm_proveed2);
    $o_BaseImArticuloFN->setUndMedida($o_propel->itm_UoM);
    $o_BaseImArticuloFN->setUoM($o_propel->itm_UoM);    
    $o_BaseImArticuloFN->setUoMCompra($o_propel->itm_UoMCompra);	
    $o_BaseImArticuloFN->setUndMedidaCompra($o_propel->itm_UoMCompra);
    $o_BaseImArticuloFN->setUoMCompraFactor($o_propel->itm_UoMCompraFacto);
    $o_BaseImArticuloFN->setUoMVenta($o_propel->itm_UoMVenta);
    $o_BaseImArticuloFN->setUoMVentaFactor($o_propel->itm_UoMVentaFacto);
    $o_BaseImArticuloFN->setGarantia($o_propel->itm_garantia);
    $o_BaseImArticuloFN->setInventariable($o_propel->itm_inventariable);
    $o_BaseImArticuloFN->setSerializable($o_propel->itm_serializable);
    $o_BaseImArticuloFN->setParaCompra($o_propel->itm_paraCompra);
    $o_BaseImArticuloFN->setParaVenta($o_propel->itm_paraVenta);
    $o_BaseImArticuloFN->setImpuesto1Sw($o_propel->itm_impuesto1_sw);
    $o_BaseImArticuloFN->setImpuesto2Sw($o_propel->itm_impuesto2_sw);
    $o_BaseImArticuloFN->setImpuesto3Sw($o_propel->itm_impuesto3_sw);
    $o_BaseImArticuloFN->setAdvalorem($o_propel->itm_advalorem);
    $o_BaseImArticuloFN->setPeso($o_propel->itm_peso);
    $o_BaseImArticuloFN->setVolumen($o_propel->itm_volumen);
    $o_BaseImArticuloFN->setPrecioDefault($o_propel->itm_precioDefault);
    $o_BaseImArticuloFN->setCostoProm($o_propel->itm_costoProm);
    $o_BaseImArticuloFN->setCostoUlt($o_propel->itm_costoUlt);
    $o_BaseImArticuloFN->setStockMin($o_propel->itm_stockMin);
    $o_BaseImArticuloFN->setStockMax($o_propel->itm_stockMax);
    $o_BaseImArticuloFN->setTiempoReposic($o_propel->itm_tiempoReposic);
    $o_BaseImArticuloFN->setActivo($o_propel->itm_activo);
    $o_BaseImArticuloFN->setCreaUsr($o_propel->itm_crea_usr);
    $o_BaseImArticuloFN->setCreaFecha($o_propel->itm_crea_fecha);
    $o_BaseImArticuloFN->setModifUsr($o_propel->itm_modif_usr);
    $o_BaseImArticuloFN->setModifFecha($o_propel->itm_modif_fecha);
    return $o_BaseImArticuloFN;
  }
}